import request from "@/utils/request";

const USER_BASE_URL = "/user";

const UserAPI = {
  getRoutes() {
    return request<any, any[]>({
      url: `${USER_BASE_URL}/getMenuRoutes`,
      method: "get",
    });
  },

  getInfo() {
    return request<any, UserInfo>({
      url: `${USER_BASE_URL}/profile`,
      method: "get",
    });
  },

  getPage(queryParams: UserPageQuery) {
    return request<any, PageResult<UserPageVO[]>>({
      url: `${USER_BASE_URL}/list`,
      method: "get",
      params: queryParams,
    });
  },

  getFormData(userId: number) {
    return request<any, UserForm>({
      url: `${USER_BASE_URL}/getUserInfoById/${userId}`,
      method: "get",
    });
  },

  add(data: UserForm) {
    return request({
      url: `${USER_BASE_URL}` + "/create",
      method: "post",
      data: data,
    });
  },

  update(id: number, data: UserForm) {
    return request({
      url: `${USER_BASE_URL}` + "/update",
      method: "post",
      data: data,
    });
  },

  resetPassword(id: number, password: string) {
    return request({
      url: `${USER_BASE_URL}/resetPassword/${id}`,
      method: "post",
      params: { userPassword: password },
    });
  },

  deleteByIds(ids: string) {
    return request({
      url: `${USER_BASE_URL}/delete/${ids}`,
      method: "get",
    });
  },

  getProfile() {
    return request<any, UserProfileVO>({
      url: `${USER_BASE_URL}/profile`,
      method: "get",
    });
  },

  updateProfile(data: UserProfileForm) {
    return request({
      url: `${USER_BASE_URL}/profile`,
      method: "post",
      data: data,
    });
  },

  updateUserProfile(key: string, value: string) {
    return request({
      url: `${USER_BASE_URL}/updateUserProfile`,
      method: "post",
      params: { key: key, value: value },
    });
  },

  changePassword(data: PasswordChangeForm) {
    return request({
      url: `${USER_BASE_URL}/changePassword`,
      method: "post",
      data: data,
    });
  },

  getOptions() {
    return request<any, OptionType[]>({
      url: `${USER_BASE_URL}/options`,
      method: "get",
    });
  },

  sendActiveCode(userEmail: string) {
    return request({
      url: `${USER_BASE_URL}/sendActiveCode/${userEmail}`,
      method: "post",
    });
  },
  resetForgotPassword(data: ResetPasswordForm) {
    return request({
      url: `${USER_BASE_URL}/resetForgotPassword`,
      method: "post",
      data: data,
    });
  },

  getMyRegionList() {
    return request<any, any>({
      url: `${USER_BASE_URL}/getMyRegionList`,
      method: "get",
    });
  },

  uploadAvatar(data: UserAvatar) {
    return request({
      url: `${USER_BASE_URL}/uploadAvatar`,
      method: "post",
      data: data,
    });
  },

  getActiveUserList() {
    return request<any, UserPageVO>({
      url: `${USER_BASE_URL}/getActiveUserList`,
      method: "get",
    });
  },
  transferRegionPerm(data: any) {
    return request({
      url: `${USER_BASE_URL}/transferRegionPerm`,
      method: "post",
      data: data,
    });
  },

  getRegionIdsByOtherUser(userId: number) {
    return request<any, UserInfo[]>({
      url: `${USER_BASE_URL}/getRegionIdsByOtherUser/${userId}`,
      method: "get",
    });
  },
  getUserRolesByEmail(userEmail: string) {
    return request({
      url: `${USER_BASE_URL}/getUserRolesByEmail`,
      method: "get",
      params: { email: userEmail },
    });
  },

  transferAdmin(name: string, email: string, phone: string, password: string, usertype: string) {
    return request({
      url: `${USER_BASE_URL}/transferAdmin`,
      method: "post",
      params: { email: email, name: name, phone: phone, password: password, usertype: usertype },
    });
  },

  getRolesByLogedUser() {
    return request({
      url: `${USER_BASE_URL}/getRolesByLogedUser`,
      method: "get",
    });
  },
  setNewUserToken(token: string) {
    return request({
      url: `${USER_BASE_URL}/setNewUserToken`,
      method: "post",
      params: { token: token },
    });
  },

  countAdminStatics() {
    return request({
      url: `${USER_BASE_URL}/countAdminStatics`,
      method: "get",
    });
  },
};

export default UserAPI;

export interface UserInfo {
  userId?: number;
  userFullName?: string;
  userPhone?: string;
  userEmail?: string;
  avatar?: string;
  roles?: string[];
  perms?: string[];
  userType: number;
  customerRegionId: number;
}

export interface UserPageQuery extends PageQuery {
  keywords?: string;
  userStatus?: number;
  deptId?: number;
  isAdmin: number;
  userType: number;
  customerRegionId?: number;
  createdAt?: [string, string];
}

export interface UserPageVO {
  userEmail?: string;
  userId?: number;
  userPhone?: string;
  userFullName?: string;
  userStatus?: number;
  isAdmin: number;
  userType: number;
  customerRegionId: number;
}

export interface UserForm {
  userEmail: string;
  userId?: number;
  userPhone?: string;
  userFullName?: string;
  userStatus?: number;
  deptId?: number;
  userPassword?: string;
  roleIds: number | undefined;
  userType: number;
  customerRegionId: number | undefined;
  customerRegionIds: number[] | string;
  createdAt?: string;
  changeCustomerEmailToo: boolean;
}

export interface UserProfileVO {
  userEmail?: string;
  userId?: number;
  userPhone?: string;
  userFullName?: string;
  userStatus?: number;
  userCustomerId: number;
  customerRegionId: number;
  isAdmin: number;
  avatar?: string;
  userType: number;
  hasDevice: number;
  roleName: string;
}

export interface UserAvatar {
  avatar?: string;
}

export interface UserProfileForm {
  userEmail?: string;
  userId?: number;
  userPhone?: string;
  userFullName?: string;
  userStatus?: number;
}

export interface PasswordChangeForm {
  oldPassword?: string;
  newPassword?: string;
  confirmPassword?: string;
}

export interface ResetPasswordForm {
  userEmail: string;
  activeCode: string;
  userPassword: string;
  confirmPassword: string;
}
